import pandas as pd
import numpy as np
from joblib import load
from sklearn.preprocessing import StandardScaler

class RedBloodCellDifferential:
    def __init__(self, model_path):
        # 动态获取脚本所在目录
        self.model = load(model_path)  # 将模型保存为类的属性

    def predict(self, csv_file_path):
        try:
            data = pd.read_csv(csv_file_path)
            # 假设数据的列名与模型训练时的特征列名一致
            # 如果列名不同，需要根据实际情况调整
            # 这里假设特征列的索引为 [3, 6, 8]
            X_new = data.iloc[:, [3, 6, 8]].values
            # 获取预测结果和概率
            predictions = self.model.predict(X_new)
            probabilities = self.model.predict_proba(X_new)
            print("Predictions:", predictions)
            print("Probabilities:", probabilities)

            # 添加预测结果和概率到数据框
            data['Predicted_Label'] = predictions
            for i in range(probabilities.shape[1]):
                data[f'Probability_Class_{i}'] = probabilities[:, i]

            # 保存结果到原始 CSV 文件
            data.to_csv(csv_file_path, index=False)
            print(f"\n预测结果已保存到: {csv_file_path}")
            return csv_file_path
        except Exception as e:
            print(f"发生错误: {str(e)}")  # 打印错误信息
            # 可以选择抛出异常，让调用者处理
            # raise e